package com.example.lvsuo.Execl.cw;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.*;
import java.util.Enumeration;


/**
 * 支付宝接口对接测试类
 */

@Slf4j
@EnableConfigurationProperties(ModelApplication.class)
@Controller
public class PayTestController {
    //写一个实体类, 属性和配置文件对应
    @Autowired
    ModelApplication mo;

    //沙箱应用APPID
    String APP_ID = "2021000117647989";
    //沙箱应用私钥
    String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCQJ7VYSuYgh0yJE8QEmc2G59yBvodD5BTmcUBksEU7XjuOsP8BXhl8kNgR8W8K3FJWeycl9uRVazh2XRJrncOWjEEv10xaux/U5GaVLzTR5BKp8nfD/8Szol86mnVAOKHcvlKEpYo+p2/cSeGUmm4R3JPcb16i2yVHz9+xM/UM/6Ff1wCpPCehvxsKq9uS+gBiahTW1sZppfKtjJghdMF6OyQE6V/b/ezXHogcaml1kenf/sK+wCYySVnxSRg5KMTnbuf322lKgxZXXWnHVOu4+PKvmi2njbwA7coVQRTdcO+dCZTp864bofOfHfl74+8sSnyaxxhJpKJs3amQAY7NAgMBAAECggEAELFYcYnyzg980F7GkxXQU+/je+65MSZnOSwiHca/rZlC4koirWDdKKyJKMLBbguHSVZneSD23eFVn3c1m9qf/H3vSn/u6o7Cp99u9ZWK00dK1f0q2zpx569Ewb+DFlW4gJxVZ+DXelvfOy4e/htH5a+aO6t11Pv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W7LK/U=";
    //支付宝公钥
    String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9ZHd/97z/7b853MSc3isIlWiTr7cAvV6hUsHm6HDnunxPpeDjmjcZhLDSuoQDvYrvWRANcy0yQ7ffntYAPMz0W9Q8KBuP2nB0/yps/e7H0D5Bs0rU5RMr9E9VsZwLPMw/pT/8yM+y3M63Lo3o5A6E+ehCQXwzp8s4LR0SwiI3iPveipZaERp81GIXg8Hj3/zGLj8MZm0PNyFHSj6tDE7GiacK+UQ4SWyqRMmr5EmW8kkCWWZGaCcHJgaEXq4/rbukXI4UvdM9lFprfbTgsRqNfVBYL4ijKUJ2LY+KVtqB6TpVMJUAR57rQFxc7UhgORoqsHWV7aSg9DrxeKUbcdUlwIDAQAB";
    //签名算法类型

    String CHARSET = "utf-8";
    //支付宝接口的网关地址，正式"https://openapi.alipay.com/gateway.do"
    String serverUrl = "https://openapi.alipaydev.com/gateway.do";
    //签名算法类型
    String sign_type = "RSA2";

    @GetMapping("/alipaytest")
    public void alipaytest(HttpServletRequest httpRequet, String message,
                           HttpServletResponse httpResponse) throws ServletException, IOException {
        //构造sdk的客户端对象
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, sign_type); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
        //内网渗透
        // System.err.println("内网渗透地址" +mo.getNATAPP());
        //在公共参数中设置回跳和通知地址  //使用内网渗透
        String neiwang = mo.getNATAPP() + "/fukuanchenggong";
        //拼接出来的url是 http://"xanwvu.natappfree.cc"/fukuanchenggong 有问题 按照双引 切割
        // 然后合并为新的字符串 可去掉所有的双引
        String[] p = neiwang.split("\"");
        String zuixin = "";
        for (int i = 0; i < p.length; i++) {
            zuixin += p[i];
        }
        // System.err.println(zuixin); 新的url
        alipayRequest.setNotifyUrl(zuixin);//在公共参数中设置回跳和通知地址


        alipayRequest.setBizContent("{" + " \"out_trade_no\":\"" + System.currentTimeMillis() + "\"," + " \"total_amount\":\"" + message + "\"," + " \"subject\":\"心心念念事务所 迟迟\"," + " \"product_code\":\"QUICK_WAP_PAY\"" + " }");//填充业务参数
        String form = "";

        try {
            //请求支付宝下单接口,发起http请求
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();

    }

    @GetMapping("/pcip")
    @ResponseBody
    public String pcip() throws UnknownHostException {
        //获取电脑的ip
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface
                    .getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
                        .nextElement();

                // 去除回环接口，子接口，未运行和接口
                if (netInterface.isLoopback() || netInterface.isVirtual()
                        || !netInterface.isUp()) {
                    continue;
                }

                if (!netInterface.getDisplayName().contains("Intel")
                        && !netInterface.getDisplayName().contains("Realtek")) {
                    continue;
                }
                Enumeration<InetAddress> addresses = netInterface
                        .getInetAddresses();
                System.out.println(netInterface.getDisplayName());
                while (addresses.hasMoreElements()) {
                    InetAddress ip = addresses.nextElement();
                    if (ip != null) {
                        // ipv4
                        if (ip instanceof Inet4Address) {
                            System.out.println("ipv4 = " + ip.getHostAddress());
                            return ip.getHostAddress();
                        }
                    }
                }
                break;
            }
        } catch (SocketException e) {
            System.err.println("Error when getting host ip address"
                    + e.getMessage());
        }
        return null;
    }





        //付款成功走的方法
    @RequestMapping ("/fukuanchenggong")
    @ResponseBody
    public   void   fukuanchenggong(){
        //修改状态为支付成功
   mo.setZhifu("true");
    }
  //前端实现轮询 判断是否支付成功
    @GetMapping  ("/lunxun")
    @ResponseBody
    public   String   lunxun(){
        return   mo.getZhifu();
    }

}